Ext.ns('Portal.teleQuestionFreeScreen');

Ext.apply(Portal.teleQuestionFreeScreen, {

    //functie de stergere / editare inregistrare
    action: function() {
        var iRec = 0,oWind;
        var oGrid = Ext.getCmp(Portal.teleQuestionFreeScreen.id).getComponent("oGrid");

        //setare tip operatie; daca este butonul de adaugare / editare atunci se citeste paramentrul `action` al acestuia
        var op = (this.action) ? this.action : "edit";

        if (op === "edit") {
            if (oGrid.selModel.getCount() != 1) {
                Portal.events.throwInformation("Selectati o inregistrare !");
                return;
            }
            iRec = oGrid.selModel.getSelected().id;
        }

        //constructie formular
        var oFpAdd = new Ext.FormPanel({
            autoHeight: true, //inaltimea formului va da inaltimea ferestrei
            labelWidth: 140,
            frame : true,
            bodyStyle: {
                padding : "5px"
            },
            layout: 'form',
            defaults: {
                anchor: '90%',
                xtype: "textfield",
                allowBlank: false
            },
            items: [
                {
                    name: 'FQuestionName',
                    fieldLabel: 'Intrebare'   ,
                    xtype: "textarea"
                },
                {
                    fieldLabel : "Campanie",
                    anchor: '90%',
                    xtype : "uxFCombo",
                    allowBlank: false,
                    editable : true,
                    name : "IdCampaign",
                    easyConfig: {
                        baseParams : {
                            activ : "1"
                        } ,
                        readerFields: [
                            { name : "Id",  mapping : "Id"  },
                            { name : "Name", mapping : "Name" }
                        ]  ,
                        proxyUrl:  'Campaign/GetCampaign'
                    }
                },
                {
                    xtype: 'xcheckbox',
                    name: "tqf_active",
                    fieldLabel : "Disponibil"
                },
                {
                    name : "IdFQuestion",
                    xtype : "hidden",
                    value : iRec
                }
            ],
            buttons: [
                {
                    text: 'Salvare',
                    handler: function() {

                        if (!oFpAdd.getForm().isValid()) {
                            return;
                        }

                        oFpAdd.getForm().submit({
                            url: "TQuestion/EditFQuestion",
                            success: function(form, response) {
                                oGrid.store.reload();
                                oWind.close();
                            }
                        });
                    }
                }
            ]
        });

        //constructie fereastra
        oWind = new Ext.Window({
            title: (op === "edit") ? "Editare inregistrare" : "Adaugare inregistrare",
            width: 450,
            layout: 'fit',
            modal: true,
            closeAction: 'close',
            items: [oFpAdd]
        }).show();

        //daca este editare de inregistrare atunci se aduc datele acestei inregistrari
        if (op === "edit") {
            oFpAdd.on("afterlayout", function() {
                        Ext.Ajax.request({
                            url: 'TQuestion/GetFQuestionById',
                            params: {
                                iRec :  iRec
                            },
                            success: function(response) {
                                var oRes = Ext.decode(response.responseText);
                                var oData = oRes.Records;
                                fnCompleteForm(oFpAdd, oData[0]);  //vezi utils.js
                            }})
                    }, this, {single: true});
        }
    }
});

Ext.apply(Portal.teleQuestionFreeScreen, {

    id: 'idTabteleQuestionFreeScreen',
    exec: function() {

        if (!Ext.getCmp(Portal.teleQuestionFreeScreen.id)) {
            var oCm = new Ext.grid.ColumnModel({
                columns: [
                    {
                        header: "Campanie",
                        dataIndex: 'CampaignName',
                        width: 200
                    },
                    {
                        header: "Intrebare",
                        dataIndex: 'FQuestionName' ,
                        width : 400
                    },
                    {
                        header: 'Activ',
                        dataIndex: 'tqf_active',
                        width: 50,
                        renderer: Portal.renderer.available
                    }
                ],
                defaults: {
                    sortable: true
                }
            });

            var oGrid = new Ext.ux.fastGrid({
                xtype: "uxFGrid",
                layout: 'fit',
                itemId: "oGrid",
                region: 'center',
                gridConfig: {
                    url: 'TQuestion/GetFQuestion' ,
                    fields: ['IdFQuestion', 'FQuestionName', 'CampaignName','IdCampaign', 'tqf_active']
                },
                cm: oCm,
                tbar: [
                    {
                        text: "Adauga",
                        iconCls: 'icon-fugue-plus-circle',
                        action: "add",
                        handler: Portal.teleQuestionFreeScreen.action
                    },
                    "-",
                    {
                        text: "Editeaza",
                        iconCls: 'icon-fugue-pencil',
                        action: "edit",
                        handler: Portal.teleQuestionFreeScreen.action
                    } ,
                    "-",
                    {
                        xtype: "uxFCombo",
                        emptyText  : "Selectati campania",
                        editable: true,
                        name: "client_id",
                        listWidth : 300,
                        easyConfig: {
                            readerFields: [
                                { name : "Id",  mapping : "Id"  },
                                { name : "Name", mapping : "Name" }
                            ]  ,
                            proxyUrl: 'Campaign/GetCampaign'
                        }   ,
                        listeners : {
                            select : function() {
                                oGrid.store.reload({
                                    params  : {
                                        client_id : this.getValue()
                                    }
                                });
                            }
                        }
                    }
                ],
                listeners: {
                    'rowdblclick': Portal.teleQuestionFreeScreen.action
                }
            });

            //creare tab ce contine gridul
            var oNomTab = new Ext.Panel({
                id: Portal.teleQuestionFreeScreen.id,
                iconCls: 'icon-fugue-question-frame' ,
                title: 'Intrebari cu raspuns liber',
                closable: true,
                layout: 'border',
                items: [oGrid]
            });

            //adaugare tab la tabpanel-ul principal
            Portal.oCenterRegion.add(oNomTab);
            Portal.oCenterRegion.doLayout();

        }
        //activare tab
        Portal.oCenterRegion.setActiveTab(Portal.teleQuestionFreeScreen.id);
    }
});